/**
 * 功能描述
 */
define(function (require, exports, module) {
    //引入视图基类
    var ELView = require("elview");
    var handlebars = require("handlebars");  
    //assets
    var template = require("./assets/tpl/productitem_main.tpl");
    require("./assets/css/productitem_main.css");
    var productSubnavComp = require("./component/productSubnavComp");
    var productNodata = require("./component/productNodata");
    
    var productMenu = require("./component/productMenu");
    var productItems = require("./component/productItems");
    var cartEvents = require("./handler/cartEvents");
    var productCart = require("./component/productCart");
    var productPageCtrl = require("./component/productPageCtrl");
    var productPayType = require("./component/productPayType");
    var payEvents = require("./handler/payEvents");
    var paySuccess = require("./component/productPaySuccess");
    var tools = require("tools");
    var isLeftMenuClick = false;
    var baseTimer = null;//防止页面失效
    
    var App = ELView.extend({
        attrs: {
            template: template
        },
        events:{
        	//选择服务项目菜单
    		"click #serviceitems":function(e){
    			if(productSubnavComp.click(e)){
    				productMenu.initMenu(this);
    			}
    		},
    		//选择商品菜单
    		"click #goods":function(e){
    			if(productSubnavComp.click(e)){
    				productMenu.initGoodsMenu(this);
    			}
    		},
    		"click li.menu":function(e){
    			productMenu.clickMenu(e,this);
    		},
    		"click .list-group-item":function(e){
    			var typePk = $(e.currentTarget).attr("data-pk");
    			isLeftMenuClick = true;
    			 $('html, body').animate({
                     scrollTop: $("div[name='group_"+typePk+"']")[0].offsetTop
                 }, 500,
                 function(){
                	 isLeftMenuClick = false;
                 });  
    		},
    		//添加商品到购物车
    		"click .add-item":function(e){
    			var data={
    				pk:$(e.currentTarget).attr("data-pk"),
    				type:$(e.currentTarget).attr("data-type")
    			};
    			cartEvents.addCart(data,this);
    		},
    		//购物车图片点击事件
    		"click #cart":function(e){
    			if($("#shooping-cart").is(':hidden')){
    				$("#shooping-cart").show(300);
    			}else{
    				$("#shooping-cart").hide(300);
   				}
    		},
    		//购物车清空
    		"click #removeAll":function(e){
    			cartEvents.removeAll(this);
    		},
    		"click .item-num-add":function(e){
    			var data={
        				pk:$(e.currentTarget).attr("data-pk"),
        				type:$(e.currentTarget).attr("data-type")
        			};
        		cartEvents.addCart(data,this);
    		},
    		//购物车减少事件
    		"click .item-num-decrease":function(e){
    			var data={
        				pk:$(e.currentTarget).attr("data-pk"),
        				type:$(e.currentTarget).attr("data-type")
        			};
        		cartEvents.decreaseCart(data,this);
    		},
    		//删除购物车商品
    		"click .clean-cart-item":function(e){
    			var data={
        				pk:$(e.currentTarget).attr("data-pk"),
        				type:$(e.currentTarget).attr("data-type")
        			};
        		cartEvents.removeItem(data,this);
    		},
    		//显示结算页面
    		"click #goSettlement":function(e){
//    			productPageCtrl.showOrderConfirm(this);
    			productPayType.initPayPage(this);
    			productPageCtrl.showChangePayType(this);
    		},
    		//显示支付方式选择页面
    		"click #settlement":function(e){
    			productPayType.initPayPage(this);
    			productPageCtrl.showChangePayType(this);
    		},
    		//显示支付成功页面
    		"click #orderPay":function(e){
    			
    			payEvents.pay(this);
    			
    		},
    		"click .full-right-menu":function(e){
    			//TODO 全屏
    			if($(".exitFullScreen").hasClass("hidden")){
    				tools.requestFullscreen(true);
    				$(".requestFullScreen").addClass("hidden");
    				$(".exitFullScreen").removeClass("hidden");
    				$(".el-pos-productitem .J-subnav").css("position","fixed");
    			}else{
    				tools.exitFullscreen(true);
    				$(".exitFullScreen").addClass("hidden");
    				$(".requestFullScreen").removeClass("hidden");
    				$(".el-pos-productitem .J-subnav").css("position","absolute");
    			}
    		},
    		//从支付返回订单
    		"click #return-order-menu":function(e){
    			productPageCtrl.returnChangePayType(this);
    		},
    		//从订单返回主页
    		"click #return-home-menu":function(e){
    			productPageCtrl.returnHome(this);
    		},
    		//支付方式按钮点击事件
    		"click .pay-type-button":function(e){
    			productPayType.selectPayType(e,this);
    		},
    		"keypress #cardNumber":function(e){
    			if(e.keyCode==13) {
    				payEvents.getMemberInfoByCard(this,e.target.value);
    				e.stopPropagation();
        			e.preventDefault();
    		    }
    		},
    		"keypress #idNumber":function(e){
    			if(e.keyCode==13) {
    				payEvents.getMemberInfoByIdnumber(this,e.target.value);
    				e.stopPropagation();
        			e.preventDefault();
    		    }
    		},
    		//显示明细页面
    		"click .item-image":function(e){
    			var data={
        				pk:$(e.currentTarget).attr("data-pk"),
        				type:$(e.currentTarget).attr("data-type")
        			};
    			productPageCtrl.showDetails(this,data);
    		},
    		//从明细页面返回主页
    		"click #details-return-home-menu":function(e){
    			productPageCtrl.returnHomeByDetails(this);
    		},
    		//从支付成功页返回主页
    		"click .to-home-button":function(e){
    			productPageCtrl.returnHomeByButton(this);
    		}
    	},
        initComponent: function (params, widget) {
        	productSubnavComp.initComponent(params, widget);
        	productPayType.initPayPage(widget);
        	paySuccess.initPaySuccessPage(widget);
        },
        afterInitComponent: function (params, widget) {
        	productMenu.initMenu(widget);
        	productCart.initCart(widget);
        	//打开节点清空购物车
        	cartEvents.removeAll(widget);
        	//类目滚动反向关联菜单
        	$(".product-items").on('scroll',function(e){
        		if(isLeftMenuClick){
        			return;
        		}
        		var domArr = $(".product-items div[name^='group_']");
        		for(var i=0;i<domArr.length;i++){
        			var offset = $(domArr[i]).offset();
        			var top = offset.top - 98;
        			
        			if(top>-100 && top<200){
        				var typePk = $(domArr[i]).attr("data-typePk");
        				var ele = $("li.menu[data-pk='"+typePk+"']");
        				
        				if(!$(ele).hasClass("active")){
        					$("li.menu").removeClass("active");
        					$(ele).addClass("active");
        				}else{
        					
        				}
        			}
        			
        		}
        	});
        	//防止页面长时间不动失去session
        	baseTimer = setInterval(function() {
        		payEvents.userMe();
            }, 1800000);
        	
        },
        destroy: function(){
			App.superclass.destroy.call(this,arguments);
			clearInterval(baseTimer);
		}
    });

    module.exports = App;
});
